﻿Imports System.Data.OleDb
Public Class AddProject
    Inherits System.Web.UI.Page
    Protected Sub btn_Submit_Click(sender As Object, e As EventArgs) Handles btn_Submit.Click

        'ADDING NEW PROJECT
            ''Unique Name
            Dim newFileName As String
            newFileName = Guid.NewGuid().ToString() & getSuffix(fu_Preview.FileName)
        ''Check for file presence
            If fu_Preview.HasFile Then
                fu_Preview.SaveAs(Server.MapPath("Pictures") & "/" & newFileName)
            End If

            ''SQL Command String
            Dim sql As String = "INSERT INTO Project (Title,Author,Difficulty,Components,Costs,Duration,Instructions,Preview) VALUES (@f1,@f2,@f3,@f4,@f5,@f6,@f7,@f8)"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ArduinoPlaygroundConnectionString").ConnectionString)
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

            ''Value Substitution
            cmd.Parameters.AddWithValue("@f1", txtbox_ProjectTitle.Text)
            cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
            cmd.Parameters.AddWithValue("@f3", ddl_Difficulty.SelectedValue)
            cmd.Parameters.AddWithValue("@f4", txtbox_Components.Text)
            cmd.Parameters.AddWithValue("@f5", ddl_Costs.SelectedValue)
            cmd.Parameters.AddWithValue("@f6", ddl_Durations.SelectedValue)
            cmd.Parameters.AddWithValue("@f7", txtBox_Instructions.Text)
            cmd.Parameters.AddWithValue("@f8", "Pictures/" & newFileName)

            ''Connection Execution
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
            cmd.Dispose()
            conn.Dispose()
            Response.Redirect("list.aspx")
    End Sub

    ''FUNCTION TO SPLIT FILE NAME
    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return suffix
    End Function
End Class